package com.zhiyou.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.entity.Activity;
import com.zhiyou.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Activity")
public class ActivityController {

    @Autowired
    ActivityService activityService;

    @RequestMapping("/getAll")
    public String getAll(@RequestParam(value = "pn",defaultValue = "1") Integer pn,
                         @RequestParam(value = "field",required = false) String field,
                         @RequestParam(value = "keyword",required = false)String keyword, Model model){
        PageHelper.startPage(pn,20);
//        System.out.println("field="+field+"---"+keyword);
        List<Activity> all = activityService.getAll(field,keyword);
        PageInfo<Activity> list = new PageInfo<>(all);
        model.addAttribute("activityList",list);
        return "Activity/list";
    }

    @RequestMapping("/add")
    public String add(Activity activity,Model model){
       /* activity.setaTaketime(new Date());*/
        activityService.add(activity);
        return "redirect:/Activity/getAll?pn=1";
    }

    @RequestMapping("/update")
    public String update(Activity activity){
        System.out.println(activity);
        activityService.update(activity);
        return "redirect:/Activity/getAll?pn=1";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer aId){
        System.out.println(aId);
        activityService.delete(aId);
        return "redirect:/Activity/getAll?pn=1";
    }
}
